home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Bitwise / bitwiseclass.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  1.5 KB  |  67 lines

  1. using System;
  2.  
  3.     /// <summary>
  4.     /// ByteBitWise, WordBitWise, DWordBitWise are classes to support
  5.     /// shl and shr operations at byte, word (UInt16) or DWORD (UInt32) level not
  6.     /// defined for C#'s .cs by default.
  7.     /// made by Endre I. Simay,
  8.     /// Hungary
  9.     /// </summary>
  10. public class ByteBitWise {
  11.     byte [] ext;
  12.     public ByteBitWise () {
  13.         ext =new byte [8];
  14.         ext[0]=1;
  15.         for (int i=1; i<8; i++) // {1, 2, 4, 8, 16, 32, 64, 128};
  16.         {
  17.             ext[i]=(byte)(ext[i-1]*2);
  18.         }
  19.     }
  20.     public byte shr (byte basic, byte n) {
  21.         return (byte)(basic/ext[n & (8-1)]);
  22.     }
  23.  
  24.     public byte shl (byte basic, byte n) {
  25.         return (byte)(basic*ext[n & (8-1)]);
  26.     }
  27. }
  28.  
  29. public class WordBitWise {
  30.     UInt16 [] ext;
  31.     public WordBitWise () {
  32.         ext =new UInt16 [16];
  33.         ext[0]=1;
  34.         for (int i=1; i<16; i++) // {1, 2, 4, 8, 16, 32, 64, 128...};
  35.         {
  36.             ext[i]=(UInt16)(ext[i-1]*2);
  37.         }
  38.     }
  39.     public UInt16 shr (UInt16 basic, UInt16 n) {
  40.         return (UInt16)(basic/ext[n & (16-1)]);
  41.     }
  42.  
  43.     public UInt16 shl (UInt16 basic, UInt16 n) {
  44.         return (UInt16)(basic*ext[n & (16-1)]);
  45.     }
  46. }
  47.  
  48. public class DWordBitWise {
  49.     UInt32 [] ext;
  50.     public DWordBitWise () {
  51.         ext =new UInt32 [32];
  52.         ext[0]=1;
  53.         for (int i=1; i<32; i++) // {1, 2, 4, 8, 16, 32, 64, 128...};
  54.         {
  55.             ext[i]=(UInt32)(ext[i-1]*2);
  56.         }
  57.     }
  58.     public UInt32 shr (UInt32 basic, UInt32 n) {
  59.         return (UInt32)(basic/ext[n & (32-1)]);
  60.     }
  61.  
  62.     public UInt32 shl (UInt32 basic, UInt32 n) {
  63.         return (UInt32)(basic*ext[n & (32-1)]);
  64.     }
  65. }
  66.  
  67.